//
//  SubmitOrderViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/4.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "SubmitOrderViewController.h"
#import "PayViewController.h"
#import "UseDiscountViewController.h"
#import "GotoAccountViewController.h"
@interface SubmitOrderViewController ()
@property (nonatomic, copy) NSString *orderString;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, copy) NSString *num;
@property (nonatomic, assign) float i;
@end

@implementation SubmitOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, -64, GWIDTH, 64)];
    vi.backgroundColor = ColorWhite;
    [self.view addSubview:vi];
    self.title = @"去结算";
    _i = 1;
    [self initView];
}


- (void)initView
{
    UILabel *titleName = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, GWIDTH - 90, 49)];
//    titleName.text = @"爱儿美儿童舞蹈秋季梦想儿童舞蹈";
    titleName.text = self.goodsName;
    titleName.textColor = ColorFrom16(0x7F7E7D);
    titleName.font = PFRFONT(14);
    [self.view addSubview:titleName];
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(GWIDTH - 17 - 100, 0, 100, 49)];
    priceLabel.text = @"580元";
    priceLabel.text = self.sale_price;
    priceLabel.textColor = ColorFrom16(0x7F7E7D);
    priceLabel.textAlignment = 2;
    priceLabel.font = PFRFONT(12);
    [self.view addSubview:priceLabel];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 49 , GWIDTH, 1)];
    line.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line];
    
    
    //数量
    UILabel *label = [UILabel new];
    label.text = @"数量";
    label.textColor = ColorFrom16(0x333333);
    label.font = PFRFONT(14);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(13);
        make.left.equalTo(self.view).offset(10);
        make.width.equalTo(@94);
        make.height.equalTo(@20);
    }];
    
    //加
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setTitle:@"+" forState:0];
    [addBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    addBtn.layer.borderColor = ColorFrom16(0xC2C2C2).CGColor;
    addBtn.layer.borderWidth = 1;
    addBtn.titleLabel.font = PFRFONT(12);
    [addBtn addTarget:self action:@selector(handleAddBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(12);
        make.right.equalTo(self.view).offset(-5);
        make.width.equalTo(@22);
        make.height.equalTo(@22);
    }];
    
    _numberLabel = [UILabel new];
    _numberLabel.text = @"1";
    _numberLabel.textColor = ColorFrom16(0x333333);
    _numberLabel.layer.borderColor = ColorFrom16(0xC2C2C2).CGColor;
    _numberLabel.layer.borderWidth = 1;
    _numberLabel.textAlignment = 1;
    _numberLabel.font = PFRFONT(12);
    [self.view addSubview:_numberLabel];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(addBtn.mas_left).offset(-10);
        make.top.equalTo(addBtn);
        make.width.equalTo(@50);
        make.height.equalTo(@22);
    }];
    
    //减
    UIButton *reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reduceBtn setTitle:@"-" forState:0];
    [reduceBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    reduceBtn.layer.borderColor = ColorFrom16(0xC2C2C2).CGColor;
    reduceBtn.layer.borderWidth = 1;
    reduceBtn.titleLabel.font = PFRFONT(12);
    [reduceBtn addTarget:self action:@selector(handleReduceBtn:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:reduceBtn];
    [reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(12);
        make.right.equalTo(_numberLabel.mas_left).offset(-10);
        make.width.equalTo(@22);
        make.height.equalTo(@22);
    }];
    
    UIView *line1 = [UIView new];
    line1.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(13);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];

    
    //实付金额
    UILabel *paidLabel = [UILabel new];
    paidLabel.text = @"实付金额";
    paidLabel.textColor = ColorFrom16(0x333333);
    paidLabel.font = PFRFONT(14);
    [self.view addSubview:paidLabel];
    [paidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(13);
        make.left.equalTo(self.view).offset(10);
        make.width.equalTo(@73.8);
        make.height.equalTo(@20);
    }];
    
    _moneyLabel = [UILabel new];
    _moneyLabel.text = _sale_price;
    _moneyLabel.textColor = ColorFrom16(0xFF5B7A);
    _moneyLabel.font = PFRFONT(14);
    [self.view addSubview:_moneyLabel];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(13);
        make.right.equalTo(self.view).offset(-8);
        make.width.greaterThanOrEqualTo(@36.2);
        make.height.equalTo(@20);
    }];
    
    UIView *line3 = [UIView new];
    line3.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(paidLabel.mas_bottom).offset(13);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
    
    //提交订单按钮
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"去结算" forState:0];
    [submitBtn setTitleColor:ColorFrom16(0x202020) forState:0];
    submitBtn.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    submitBtn.layer.cornerRadius = 7;
    submitBtn.titleLabel.font = PFRFONT(16);
    [submitBtn addTarget:self action:@selector(handleSubmitBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line3.mas_bottom).offset(29);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
}


//去结算
- (void)handleSubmitBtn:(UIButton *)sender
{
//    PayViewController *payVC = [PayViewController new];
//    payVC.orderId = self.orderString;
//    payVC.goodsName = self.goodsName;
//    payVC.sale_price = self.sale_price;
//
    GotoAccountViewController *gotoVC = [GotoAccountViewController new];
    gotoVC.goodsName = self.goodsName;
    gotoVC.sale_price = self.sale_price;
    gotoVC.number = _numberLabel.text;
    gotoVC.goodId = self.goodId;
    [UIView animateWithDuration:2.0 animations:^{
        [self.navigationController pushViewController:gotoVC animated:YES];

    }];
}

- (void)handleAddBtn:(UIButton *)sender
{
    _i += 1;
    _numberLabel.text = [NSString stringWithFormat:@"%.f", _i];
    
    NSString *str = [NSString stringWithFormat:@"%.2f", (_i * [self.sale_price floatValue])];
    _moneyLabel.text = str;
}

- (void)handleReduceBtn:(UIButton *)sender
{
    if(_i == 1)
    {
        _numberLabel.text = [NSString stringWithFormat:@"%d", 1];

        return;
    }
    else
    {
        _i -= 1;
        _numberLabel.text = [NSString stringWithFormat:@"%.f", _i];
        
        NSString *str = [NSString stringWithFormat:@"%.2f", ( [self.moneyLabel.text floatValue] / (_i + 1))];
        _moneyLabel.text = str;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
//    [self.navigationController.navigationBar setHidden:YES];
    self.navigationController.navigationBar.barTintColor = ColorWhite;
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = ColorWhite;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
